/**
 * Created by gim on 14-1-24.
 */

/**
 * default template
 * @type {{createNew: Function}}
 * @private
 */
_Mongoskin = {
   createNew: function(db,collectionName){
       var _mongoskin = {};

       _mongoskin.list =  function (callback) {
           db.collection(collectionName).find().toArray(function (err, models) {
               callback(err, models);
           });
       };

       _mongoskin.findById = function (id, callback) {
           db.collection(collectionName).findById(id, function (err, product) {
               callback(err, product);
           });
       };
       _mongoskin.modify = function (id, option, callback) {
//           var obj_id = db.bson_serializer.ObjectID.createFromHexString(id);
           db.collection(collectionName).update({_id: id}, {$set: option}, function (err, prod) {
               callback(err, prod);
           });
       };
       _mongoskin.delById= function (id, callback) {
           // var obj_id = db.bson_serializer.ObjectID.createFromHexString(id);
           db.collection(collectionName).removeById(id, function (err, result) {
               callback(err, result);
           });
       };
       _mongoskin.findByOpt = function (map,callback){
           db.collection(collectionName).find(map).toArray(function (err, models) {
               callback(err, models);
           });
       };
       _mongoskin.objSave = function (callback) {
           db.collection(collectionName).save(this, null, function (err, one) {
               callback(err, one);
           });
       };
       return _mongoskin;
   }
};

module.exports = _Mongoskin;